var app = getApp();
import { base64src } from '../../utils/base64src.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list:'',
    info:'',
    display: "none",
    windowWidth: 280,
    windowHeight: 461,
    shareImgSrc: null,
    img_qcord: ""
  },

  getCustomeItem: function () {
    var that = this;
    app.getUserToken(function (err, token) {
      wx.request({
        url: app.globalData.url +'/index.php?_m=shop/customers/get_item',
        data: { auth_token: token },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'post',
        success: (res) => {
          if (res.data.status == 401) {
            // 清空授权token
            app.globalData.token = null
            wx.removeStorageSync('token')
            that.getCustomeItem();
          } else if (res.data.code == 0) {

            that.setData({
              list: res.data.data.item
            })
          }
        }
      })
    })

  },

  getItem: function (e) {
    let that = this;
    app.getUserToken(function (err, token) {
     
      wx.request({
        url: app.globalData.url +'index.php?_m=shop/customers_station/get_item',
        data: { auth_token: token },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        success: (res) => {
          
          if (res.data.status == 401) {
            // 清空授权token
            app.globalData.token = null
            wx.removeStorageSync('token')
            that.formSubmit();
          } else if (res.data.code == 0) {
           
            that.setData({
              info: res.data.data.item
            })
          }
        }
      })
    })
  },

  goInfo:function(e){
    wx.navigateTo({
      url:"../gainCode/gainCode"
    })

  },

  getQcord(uid) {
    wx.showLoading({
      title: '加载中...',
    })
    let that = this;
    wx.cloud.callFunction({
      name: 'getImage',
      data: {
        page: "pages/register/register",
        id: uid
      },
      complete: res => {
        wx.hideLoading();
        let img = wx.arrayBufferToBase64(res.result.buffer);

        base64src('data:image/jpg;base64,' + img, res => {
          console.log(res);
          that.setData({
            img_qcord: res
          })
        });
      }
    })
  },
  
  onLoad: function (options) {
    this.getCustomeItem();
    this.getItem();
    this.getQcord(app.globalData.uid);
  },
  onShareTap: function (event) {  
    wx.showLoading({
      title: '生成中',
    })
    this.setData({
      display: "block",
    })
    var that = this;


    that.onshow("智汇GO", "/images/qy.png");

  },

  onshow: function (name, img) {

    var that = this;
    //1. 请求后端API生成小程序码
    //2. canvas绘制文字和图片
    const ctx = wx.createCanvasContext('myCanvas');
    //背景图

    // var bgImgPath = this.data.item.img_url;
    //var bh_bg = "../../images/hb-bg.png";
    ctx.drawImage(img, 0, 0, this.data.windowWidth, 461);

    var imgPath = that.data.img_qcord;//二维码
    ctx.drawImage(imgPath, 100, 255, 80, 80);

    //绘制字体
    // ctx.setFontSize(13)
    // ctx.setFillStyle('#6F6F6F')
    // ctx.fillText(name, 10, 338)

    // var td = Math.ceil(this.data.windowWidth / 20);
    // console.log(td);
    // var tr = Math.ceil(this.data.windowHeight / td);

    // for (var i = 0; i < tr; i++) {
    //   var txt = {
    //     x: 140,
    //     y: 310 + (i * 10),
    //     color: '#111111',
    //     size: '13',
    //     align: 'left',
    //     baseline: 'top',
    //     text: this.data.item.name.substring(i * td, 14),
    //     bold: false
    //   };
    //   if (i < 3) {
    //     if (i == 1) {
    //       //txt.text = txt.text
    //       txt.text = txt.text.substring(0, txt.text.length - 3);
    //     }
    //     ctx.save();
    //     ctx.setFontSize(13)
    //     ctx.setFillStyle('#000000')
    //     ctx.setTextAlign('center');
    //     ctx.setTextBaseline('top');
    //     ctx.fillText(txt.text, txt.x, txt.y);
    //   }
    // }
    ctx.draw(false, setTimeout(function () {
      wx.canvasToTempFilePath({
        x: 0,
        y: 0,
        width: that.data.windowWidth,
        height: that.data.windowHeight,
        destWidth: that.data.windowWidth * 3,
        destHeight: that.data.windowHeight * 3,
        canvasId: 'myCanvas',
        success: function (res) {
          console.log(res.tempFilePath);
          wx.hideLoading();
          that.setData({
            shareImgSrc: res.tempFilePath
          })

        },
        fail: function (res) {
          wx.hideLoading();
          wx.showToast({
            title: '生成失败',
            icon: "none"
          })
        }
      })
    }, 200));
  },

  showLocal: function () {
    var that = this;
    console.log(that.data.shareImgSrc);
    wx.saveImageToPhotosAlbum({
      filePath: that.data.shareImgSrc,
      success(res) {
        wx.showModal({
          title: '存图成功',
          content: '图片成功保存到相册了，去发圈噻~',
          showCancel: false,
          confirmText: '好哒',
          confirmColor: '#21bba6',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定');
            }

          }
        })
      }, fail: function () {
        wx.showToast({
          title: '保存失败',
          icon: "none"
        })
      }
    })
  },
  hideview: function () {
    this.setData({
      display: "none"
    })
  },

  onShareAppMessage: function () {
    app.globalData.uid = app.globalData.uid || wx.getStorageSync('uid') || 0;
    return {
      title: '深圳智汇创想科技有限责任公司员工福利购!',
      path: '/pages/index/index?fid=' + app.globalData.uid
    }
  }

})